//
//  StatusRetweeterView.swift
//  SinaWeibo
//
//  Created by 张洪毓 on 15/11/17.
//  Copyright © 2015年 张洪毓. All rights reserved.
//

import UIKit
import SnapKit
class StatusRetweeterView: UIView {
    
    var retweeterBottomConstraints: Constraint?
    
    var retweeterStatus: Status? {
        didSet {
            retweetLabel.attributedText = EmotionManager.emoticonManager.textToImageText(retweeterStatus?.text ?? "", font: retweetLabel.font)
            self.retweeterBottomConstraints?.uninstall()
            if let pictures = retweeterStatus?.imageURLs where pictures.count > 0 {
                pictureView.hidden = false
                pictureView.imageURLs = pictures
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweeterBottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(statusCellMargin).constraint
                })
            } else {
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweeterBottomConstraints = make.bottom.equalTo(retweetLabel.snp_bottom).offset(statusCellMargin).constraint
                })
            }
        }
    }
    
    //MARk:  3. 重写父类的构造方法，调用setupUI
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARk:  2. 设置UI
    private func setupUI() {
        addSubview(retweetLabel)
        addSubview(pictureView)
        
        retweetLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(snp_top).offset(statusCellMargin)
            make.left.equalTo(snp_left).offset(statusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweetLabel.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(retweetLabel.snp_left)
//            make.width.equalTo(100)
//            make.height.equalTo(100)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            self.retweeterBottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(statusCellMargin).constraint
        }
        
    }
    
    //MARk:  1. 懒加载所有控件
    private lazy var retweetLabel: UILabel = UILabel(title: "转发微博测试", size: 14, color: UIColor.darkGrayColor(), margin: statusCellMargin)
    private lazy var pictureView: StatusPictureView = StatusPictureView()
    
}
